package com.cxsw.rms.controller;

import com.cxsw.rms.service.SysUserService;
import com.cxsw.rms.utils.Constants;
import com.cxsw.rms.utils.ResultObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 登录控制器
 *
 * @author Han Fuhao
 * @date 2023/05/14
 */
@Controller
public class LoginController {
    /**
     * 关于系统用户业务逻辑层Spring注入
     */
    @Resource
    private SysUserService sysUserService;

    /**
     * 登录验证及数据响应
     *
     * @param username 用户名
     * @param password 密码
     * @param model    数据响应Model
     * @param session  Session会话
     * @return {@link String}
     */
    @PostMapping("login")
    public String login(String username, String password, Model model, HttpSession session){
        ResultObject resultObject = sysUserService.selectForLogin(username, password);
        if (resultObject.getCode().equals(Constants.RESULT_OBJECT_FAILED_CODE)){
            model.addAttribute("msg", resultObject.getMsg());
            return "login";
        }
        session.setAttribute(Constants.SESSION_USER, resultObject.getData());
        return "redirect:toMain";
    }
}
